package ylzy.com.yanglezhiyang;

import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;

public class GameObject {

    private static final String TAG = "GameObject";
    public int x;
    public int y;
    public int z;
    public int w;
    public int h;
    public Bitmap bmp;//纹理
    public Rect src = new Rect();
    public Rect dst = new Rect();
    public int state = 1;//状态 -1 不可见 0 地下室 1 正常显示 2 移除
    public int category = 1;// 0-6
    public Floor floor;

    public GameObject(int category, Bitmap bmp) {
        this.category = category;
        this.bmp = bmp;
    }

    public void setFloor(Floor floor) {
        this.floor = floor;
    }

    public GameObject(int x, int y, int w, int h, int z, Bitmap bmp, int state) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.w = w;
        this.h = h;
        this.bmp = bmp;
        this.state = state;

        src.left = 0;
        src.top = 0;
        src.right = bmp.getWidth();
        src.bottom = bmp.getHeight();

        dst.left = x+1;
        dst.top = y+1;
        dst.right = x + w - 1;
        dst.bottom = y + h - 1;

    }

    public void layout(int x, int y, int w, int h, int z, int state) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.w = w;
        this.h = h;
        this.state = state;

        src.left = 0;
        src.top = 0;
        src.right = bmp.getWidth();
        src.bottom = bmp.getHeight();

        dst.left = x+1;
        dst.top = y+1;
        dst.right = x + w -1;
        dst.bottom = y + h - 1;

    }

    public boolean isCovered(GameObject temp) {
        if (temp.dst.contains(x + 1, y + 1)) {
            return true;
        }
        if (temp.dst.contains(x + w - 1, y + 1)) {
            return true;
        }
        if (temp.dst.contains(x + 1, y + w - 1)) {
            return true;
        }
        if (temp.dst.contains(x + w - 1, y + w - 1)) {
            return true;
        }
        return false;
    }

    public boolean isCovered(int x, int y) {
        if (dst.contains(x, y)) {
            return true;
        }
        return false;
    }

    public void draw(Canvas canvas, Paint paint) {

        if (state == -1) {
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(0Xfff7f7f8);
            canvas.drawRect(dst, paint);

            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(0XffCCCCCC);
            paint.setStrokeWidth(1);
            canvas.drawRect(dst, paint);

        } else if (state == 1) {
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(0Xffdddddd);
            canvas.drawRect(dst, paint);

            canvas.drawBitmap(bmp, src, dst, paint);

        } else if (state == 0) {

            canvas.drawBitmap(bmp, src, dst, paint);

            paint.setStyle(Paint.Style.FILL);
            paint.setColor(0X99000000);
            canvas.drawRect(dst, paint);

        } else if (state == 2) {
            canvas.drawBitmap(bmp, src, dst, paint);
        }

    }

    public void setState(int state) {
        this.state = state;
    }

    public GameObject copy() {
        GameObject copy = new GameObject(category, bmp);
        copy.x = x;
        copy.y = y;
        copy.z = z;
        copy.w = w;
        copy.h = h;
        copy.state = 1;

        copy.src.left = 0;
        copy.src.top = 0;
        copy.src.right = bmp.getWidth();
        copy.src.bottom = bmp.getHeight();

        copy.dst.left = x - 10;
        copy.dst.top = y - 10;
        copy.dst.right = x + w + 10;
        copy.dst.bottom = y + h + 10;
        return copy;
    }

    public void move(int moveX, int moveY) {
        dst.left = dst.left + moveX;
        dst.top = dst.top + moveY;
        dst.right = dst.left + w + moveX;
        dst.bottom = dst.top + h + moveY;
    }


    int movedX = 0;
    int movedY = 0;


    public void moveToDst(View view, int dstX, int dstY, AninEndListner aninEndListner, int delay, int duration) {
        x = dstX;
        y = dstY;

        int distanceX = dst.left - dstX;
        int distanceY = dst.top - dstY;
        moveAnim(view, distanceX, distanceY, aninEndListner,delay,duration);
    }

    public void moveAnim(View view, int dcx, int dcy, AninEndListner aninEndListner, int delay, int duration) {
        movedX = 0;
        movedY = 0;
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.addUpdateListener(animation -> {
            Float mFloat = (Float) animation.getAnimatedValue();
            int moveX = (int) (-dcx * mFloat);
            int moveY = (int) (-dcy * mFloat);
            move(moveX - movedX, moveY - movedY);
            movedX = moveX;
            movedY = moveY;
            view.postInvalidate();
            if (mFloat >= 1) {
                state = 2;
                dst.left = x+20;
                dst.right = x + w - 20;
                dst.top = y+20;
                dst.bottom = y + h -20;

                if (aninEndListner != null) {
                    aninEndListner.end();
                }
            }
        });
        valueAnimator.setDuration(duration).setStartDelay(delay);
        valueAnimator.start();
    }

}
